package com.bjb.vr.operation.controller;

import com.bjb.vr.common.ao.OptCusAccountAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptCusAccountService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptCusAccountVo;
import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @version 1.0.0
 * @description:客户配置管理-账户管理
 * @author: caihaibo
 * @createDate: 2022/6/17
 */
@RestController
@Slf4j
@RequestMapping("/api/operation/v1")
public class CusAccountController {

    @Autowired
    private OptCusAccountService optCusAccountService;

    /**
     * 客户配置管理-账户管理-查询用户信息列表
     * @param optCusAccountAo
     * @return
     */
    @PostMapping("/queryCusConfigUserList")
    public CommonResult<List<OptCusAccountVo>> queryCusConfigUserList(@RequestBody OptCusAccountAo optCusAccountAo) {
        Assert.notNull(optCusAccountAo.getTenantId(), "客户租户Id不能为空");
        long start = System.currentTimeMillis();
        log.info("请求开始时间 " + start);
        List<OptCusAccountVo> userRoleVos = optCusAccountService.queryUserRoleInfo(optCusAccountAo);
        long end = System.currentTimeMillis();
        log.info("请求消耗时间 " + (end -start));
        return CommonResult.Ok().setResult(userRoleVos);
    }


    /**
     * 客户配置管理-账户管理-修改用户账号状态
     *
     * @param optCusAccountAo
     * @return
     */
    @PostMapping("/updateUserStatus")
    public CommonResult updateUserStatus(@RequestBody OptCusAccountAo optCusAccountAo) {
        Assert.notNull(optCusAccountAo.getId(), "用户ID不能为空");
        Assert.notNull(optCusAccountAo.getStatus(), "用户修改状态不能为空");
        // 禁止更新超级管理员
        boolean confirm = optCusAccountService.checkSuperAdmin(optCusAccountAo.getId(), null);
        if(confirm){
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE_SUPER_ADMIN);
        }
        // optCusAccountAo.setStatus(0);//禁用用户账号
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        boolean success = optCusAccountService.updateUserStatus(optCusAccountAo);
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 客户配置管理-账户管理-重置密码
     *
     * @param optCusAccountAo
     * @return
     */
    @PostMapping("/updateUserPassword")
    public CommonResult updateUserPassword(@RequestBody OptCusAccountAo optCusAccountAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        boolean success = optCusAccountService.updateUserPassword(optCusAccountAo);
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }
}
